package cn.stylefeng.guns.modular.recy.controller;
/*
 */

import cn.stylefeng.roses.core.base.controller.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.*;
import cn.stylefeng.guns.modular.recy.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.stylefeng.roses.core.reqres.response.*;
import cn.stylefeng.guns.modular.recy.entity.*;
import java.io.*;
import cn.stylefeng.guns.core.common.page.*;
import com.baomidou.mybatisplus.core.metadata.*;
import com.baomidou.mybatisplus.core.conditions.*;
import cn.stylefeng.guns.modular.recy.vo.*;
import cn.stylefeng.guns.modular.recy.utils.*;
import cn.hutool.core.bean.*;
import java.util.stream.*;
import cn.stylefeng.guns.modular.recy.warpper.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import java.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping({ "/wasteInfo" })
public class WasteInfoController extends BaseController
{
    private String PREFIX;
    @Autowired
    private WasteInfoService wasteInfoService;

    @Autowired
    private WasteTypeService wasteTypeService;
    
    public WasteInfoController() {
        this.PREFIX = "/modular/recy/wasteInfo/";
    }
    
    @RequestMapping({ "" })
    public String index() {
        return this.PREFIX + "wasteInfo.html";
    }
    
    @RequestMapping({ "/add" })
    public String wasteInfoAdd(Model model) {
        List<WasteType> wasteTypeList =  this.wasteTypeService.list();
        model.addAttribute("wasteTypeList",wasteTypeList);
        return this.PREFIX + "wasteInfo_add.html";
    }
    
    @RequestMapping({ "/edit" })
    public String wasteInfoUpdate(@RequestParam("id") final String id,Model model) {
        List<WasteType> wasteTypeList =  this.wasteTypeService.list();
        model.addAttribute("wasteTypeList",wasteTypeList);
        return this.PREFIX + "wasteInfo_edit.html";
    }
    
    @RequestMapping({ "/detail/{id}" })
    @ResponseBody
    public ResponseData detail(@PathVariable final String id) {
        final WasteInfo wasteInfo = (WasteInfo)this.wasteInfoService.getById((Serializable)id);
        return (ResponseData)ResponseData.success(wasteInfo);
    }
    
    @RequestMapping({ "/list" })
    @ResponseBody
    public Object list(final WasteInfo wasteInfoQuery) {
        final QueryWrapper<WasteInfo> wasteInfoQueryWrapper = ControllerUtil.createQuery(wasteInfoQuery, this.getHttpServletRequest());
        final Page<WasteInfo> page = (Page<WasteInfo>)LayuiPageFactory.defaultPage();
        final IPage<WasteInfo> page2 = (IPage<WasteInfo>)this.wasteInfoService.page((IPage)page, (Wrapper)wasteInfoQueryWrapper);
        final List<WasteInfoVO> wasteInfoVOList = EntityBind.bind(page2.getRecords(), WasteInfoVO.class);
        final List<Map<String, Object>> list = wasteInfoVOList.stream().map(wasteInfo -> BeanUtil.beanToMap(wasteInfo)).collect(Collectors.toList());
        page2.setRecords((List)new WasteInfoWrapper(list).wrap());
        return LayuiPageFactory.createPageInfo(page2);
    }
    
    @RequestMapping({ "/addItem" })
    @ResponseBody
    public Object add(final WasteInfo wasteInfo) {
        final HttpServletRequest request = this.getHttpServletRequest();
        final String header = request.getHeader("content-type");
        if (StringUtils.containsIgnoreCase((CharSequence)header, (CharSequence)"multipart/form-data")) {
            final StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
            final MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
            final Map<String, MultipartFile> fileMap = (Map<String, MultipartFile>)multipartRequest.getFileMap();
            final boolean file1 = fileMap.containsKey("file");
            if (file1) {
                final MultipartFile file2 = multipartRequest.getFile("file");
                try {
                    final String name = file2.getOriginalFilename();
                    final InputStream inputStream = file2.getInputStream();
                    String fileName = ControllerUtil.saveFile("/upload/brand", inputStream, name);
                    wasteInfo.setIcon(fileName);
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        this.wasteInfoService.save(wasteInfo);
        return WasteInfoController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/delete" })
    @ResponseBody
    public Object delete(@RequestParam final String id) {
        this.wasteInfoService.removeById((Serializable)id);
        return WasteInfoController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/editItem" })
    @ResponseBody
    public Object update(final WasteInfo wasteInfo) {
        final HttpServletRequest request = this.getHttpServletRequest();
        final String header = request.getHeader("content-type");
        if (StringUtils.containsIgnoreCase((CharSequence)header, (CharSequence)"multipart/form-data")) {
            final StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
            final MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
            final Map<String, MultipartFile> fileMap = (Map<String, MultipartFile>)multipartRequest.getFileMap();
            final boolean file1 = fileMap.containsKey("file");
            if (file1) {
                final MultipartFile file2 = multipartRequest.getFile("file");
                try {
                    final String name = file2.getOriginalFilename();
                    final InputStream inputStream = file2.getInputStream();
                    String fileName = ControllerUtil.saveFile("/upload/brand", inputStream, name);
                    wasteInfo.setIcon(fileName);
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        this.wasteInfoService.updateById(wasteInfo);
        return WasteInfoController.SUCCESS_TIP;
    }
    
    @RequestMapping(value = { "/delItems" }, method = { RequestMethod.POST })
    @ResponseBody
    public Object delItem(@RequestBody final String[] ids) {
        this.wasteInfoService.removeByIds((Collection)Arrays.asList(ids));
        return WasteInfoController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/show" })
    public Object show(final String id, final Model model) {
        final WasteInfo wasteInfo = (WasteInfo)this.wasteInfoService.getById((Serializable)id);
        final Map map = BeanUtil.beanToMap(wasteInfo);
        model.addAttribute("wasteInfo", new WasteInfoWrapper(map).wrap());
        return this.PREFIX + "wasteInfo_show.html";
    }
}
